package com.kili.recommend.data.util;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.kili.recommend.data.config.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Gordon
 * @version 2019/5/28
 */
@Slf4j
public class S3ClientUtils {
    private static String bucketName;
    private static String secretKey;
    private static String accessKey;
    private static String regions;
    private static String endpoint;
    private static AmazonS3 s3;

    static {
        Environment environment = SpringContextHolder.getApplicationContext().getEnvironment();
        regions = environment.getProperty("s3.regions", String.class);
        endpoint = environment.getProperty("s3.endpoint", String.class);
        accessKey = environment.getProperty("s3.access.key", String.class);
        secretKey = environment.getProperty("s3.access.secret", String.class);
        bucketName = environment.getProperty("s3.bucket.name", String.class);

        initClient();
        createBucketIfNot();
    }


    public static void upload(InputStream is, String key) throws IOException {
        try {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(is.available());
            // 指定该Object被下载时的网页的缓存行为
            metadata.setCacheControl("no-cache");
            // 指定该Object下设置Header
            metadata.setHeader("Pragma", "no-cache");
            // 指定该Object被下载时的内容编码格式
            metadata.setContentEncoding("utf-8");
            s3.putObject(bucketName, key, is, metadata);
        } finally {
            is.close();
        }
    }

    private static void initClient() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(credentials);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProtocol(Protocol.HTTP);
        s3 = AmazonS3ClientBuilder.standard()
                .withRegion(regions)
                .withCredentials(provider)
                .withClientConfiguration(clientConfiguration)
//                    .enablePathStyleAccess()
//                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, regions))
                .build();
    }

    private static void createBucketIfNot() {
        try {
            if (!s3.doesBucketExistV2(bucketName)) {
                s3.createBucket(bucketName);
            }
        } catch (Exception ex) {
            log.error("S3 init create bucket error", ex);
        }
    }

}
